package com.jdkreader.concurrency.queue;

import java.util.concurrent.BlockingQueue;


/**
 * 使用阻塞队列管理消费行为
 */
public class LiftOffRunner implements Runnable{

    private BlockingQueue<LiftOff> rockets;

    public LiftOffRunner(BlockingQueue<LiftOff> rockets) {
        this.rockets = rockets;
    }

    /**
     * 封装添加方法
     * @param lo
     */
    public void add(LiftOff lo){
        try{
            rockets.put(lo);
        }catch(InterruptedException e){
            e.printStackTrace();
            System.out.println("interrupt during add()");
        }
    }

    @Override
    public void run() {

         try{
             while(!Thread.interrupted()){
                 LiftOff rocket=rockets.take();
                 rocket.run();
             }
         } catch(InterruptedException e){
             e.printStackTrace();
         }
        System.out.println("Exiting liftOffRunner");
    }
}
